cmake_minimum_required(VERSION 3.22.1)

# 设置目标架构参数，必须在project()之前
set(TARGET_ARCH "x64" CACHE STRING "Target architecture (x64 or arm64)")
set_property(CACHE TARGET_ARCH PROPERTY STRINGS "x64" "arm64")

# 交叉编译设置必须在project()调用之前
if(TARGET_ARCH STREQUAL "arm64")
    message(STATUS "Configuring for ARM64 cross-compilation")
    set(CMAKE_SYSTEM_NAME Linux)
    set(CMAKE_SYSTEM_PROCESSOR aarch64)
    
    # 不使用find_program，依赖命令行参数或环境变量
    if(NOT CMAKE_C_COMPILER)
        set(CMAKE_C_COMPILER "aarch64-linux-gnu-gcc")
    endif()
    
    if(NOT CMAKE_CXX_COMPILER)
        set(CMAKE_CXX_COMPILER "aarch64-linux-gnu-g++")
    endif()
    
    message(STATUS "Using C compiler: ${CMAKE_C_COMPILER}")
    message(STATUS "Using CXX compiler: ${CMAKE_CXX_COMPILER}")
    
    set(ARCH_SUFFIX "aarch64")
else()
    message(STATUS "Configuring for x64 native compilation")
    set(ARCH_SUFFIX "x64")
endif()

project("streampush")

option (USE_MPP "Use MPP" OFF)

set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -O3  -Wall")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3  -Wall")

if(TARGET_ARCH STREQUAL "arm64")
        include_directories(${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_aarch64_pic_build/include)
        include_directories(${CMAKE_SOURCE_DIR}/../3rd_party/x264/x264_arm64_build/include)
        message(STATUS "${TARGET_ARCH}, FFmpeg path: ${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_aarch64_pic_build/include")
else()
        include_directories(${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_x64_pic_build/include)
        include_directories(${CMAKE_SOURCE_DIR}/../3rd_party/x264/x264_x64_build/include)
        message(STATUS "${TARGET_ARCH}, FFmpeg path: ${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_x64_pic_build/include")
endif()

include_directories(${CMAKE_SOURCE_DIR}/../3rd_party/libyuv/include)
include_directories(${CMAKE_SOURCE_DIR})

#zlmediakit
set(ZLMediaKit_Root ${CMAKE_SOURCE_DIR}/../3rd_party/zlmedaikit)

if(TARGET_ARCH STREQUAL "arm64")
    set(ENABLE_OPENSSL OFF CACHE BOOL "Disable OpenSSL for cross-compilation" FORCE)
    set(ENABLE_MYSQL OFF CACHE BOOL "Disable MySQL for cross-compilation" FORCE)
    set(ENABLE_MP4 ON CACHE BOOL "Enable MP4" FORCE)
    set(ENABLE_HLS ON CACHE BOOL "Enable HLS" FORCE)
endif()


add_subdirectory(${ZLMediaKit_Root} ${CMAKE_BINARY_DIR}/zlmediakit)
include_directories(${ZLMediaKit_Root}/src)
include_directories(${ZLMediaKit_Root}/srt)
include_directories(${ZLMediaKit_Root}/webrtc)
include_directories(${ZLMediaKit_Root}/server)
include_directories(${ZLMediaKit_Root}/3rdpart)
include_directories(${ZLMediaKit_Root}/3rdpart/media-server)
include_directories(${ZLMediaKit_Root}/3rdpart/ZLToolKit/src)

set(SOURCE_FILE
        streamPushWrapper.cpp
        LiveServer.cpp
        LiveService.cpp
        FFMpegPusher.cpp
        ZYUtils/ZYLog.cpp
)

add_library(${CMAKE_PROJECT_NAME} SHARED
        ${SOURCE_FILE}
)

if(TARGET_ARCH STREQUAL "arm64")
        target_link_libraries(${CMAKE_PROJECT_NAME}
                # z
                #ZLMediaKit####
                -Wl,--start-group ${MK_LINK_LIBRARIES} -Wl,--end-group
                ${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_aarch64_pic_build/lib/libavformat.a  
                ${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_aarch64_pic_build/lib/libavcodec.a  
                ${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_aarch64_pic_build/lib/libavfilter.a  
                ${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_aarch64_pic_build/lib/libavutil.a 
                ${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_aarch64_pic_build/lib/libswresample.a  
                ${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_aarch64_pic_build/lib/libswscale.a 
                ${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_aarch64_pic_build/lib/libavformat.a  
                ${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_aarch64_pic_build/lib/libswresample.a  
                ${CMAKE_SOURCE_DIR}/../3rd_party/x264/x264_arm64_build/lib/libx264.a
                ${CMAKE_SOURCE_DIR}/../3rd_party/libyuv/libyuv.a
        )
else()
        target_link_libraries(${CMAKE_PROJECT_NAME}
                z
                -Wl,--start-group ${MK_LINK_LIBRARIES} -Wl,--end-group
                ${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_x64_pic_build/lib/libavformat.a  
                ${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_x64_pic_build/lib/libavcodec.a  
                ${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_x64_pic_build/lib/libavfilter.a  
                ${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_x64_pic_build/lib/libavutil.a 
                ${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_x64_pic_build/lib/libswresample.a  
                ${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_x64_pic_build/lib/libswscale.a 
                ${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_x64_pic_build/lib/libavformat.a  
                ${CMAKE_SOURCE_DIR}/../3rd_party/ffmpeg/ffmpeg_x64_pic_build/lib/libswresample.a  
                ${CMAKE_SOURCE_DIR}/../3rd_party/x264/x264_x64_build/lib/libx264.a
                ${CMAKE_SOURCE_DIR}/../3rd_party/libyuv/libyuv.a
        )
endif()

if(TARGET_ARCH STREQUAL "x64")
        find_package( OpenCV REQUIRED )
        include_directories( ${OpenCV_INCLUDE_DIRS} )
        add_executable( DemoPush ../demo/test.cpp )
        target_link_libraries( DemoPush 
                        ${OpenCV_LIBS} 
                        streampush)
endif()